import sys
INF = 1000000

def Dijkstra(G, S):
  global INF
  dist = [INF for v in G]
  previous = [None for v in G]
  dist[S] = 0
  Q = range(len(G))
  while Q:
    dist_min, u = INF, None
    for w in Q:
      if dist[w] < dist_min:
        dist_min, u = dist[w], w
    if dist[u] == INF:
      break
    Q = [q for q in Q if q != u]
    for v, d in enumerate(G[u]):
      if d == INF:
        continue
      alt = dist[u] + G[u][v]
      if alt < dist[v]:
        dist[v] = alt
        previous[v] = u
  return dist

def main():
  global INF

  # first, normalize sensor names
  s1, s2 = sys.argv[1], sys.argv[2]
  c1, c2 = s1[0], s2[0]
  n1, n2 = int(s1[1:]), int(s2[1:])
  if not n1&1:
    n1 -= 1
  if not n2&1:
    n2 -= 1
  s1, s2 = c1+str(n1), c2+str(n2)

  # now, load the weighted graph file in
  G = [[INF for j in xrange(70)] for i in xrange(70)]
  id = {}
  last = -1
  for line in sys.stdin:
    toks = line.strip().split()
    if len(toks) >= 7:
      last = int(toks[0])
      id[toks[1]] = last
    elif len(toks) == 3:
      N = int(toks[0])
      dist = int(toks[2])
      G[last][N] = dist
      G[N][last] = dist

  # now, find shortest path from s1 to s2
  S, F = id[s1], id[s2]
  D = Dijkstra (G, S)
  print D[F]

if __name__ == '__main__':
  main()
